package me.whf.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import me.whf.model.Employee;
import me.whf.util.SQLHelper;
import me.whf.util.Utils.EmployeeType;

public class DatabaseEmployeeDAO implements EmployeeDAO {

	@Override
	public boolean isExisted(String employeeID) {
		SQLHelper helper = new SQLHelper();
		List<HashMap<String, Object>> result = helper.getQueryList("select count(*) from employee where employeeID='" + employeeID + "'");
		if (result != null && result.size() != 0) {
			long count = (long) result.get(0).get("count(*)");
			return count == 0? false: true;
		} else {
			return true;
		}
	}

	@Override
	public boolean register(String employeeID, String password,
			String username, String apartmentID, EmployeeType employeeType) {
		SQLHelper helper = new SQLHelper();
		ArrayList<String> sqls = new ArrayList<String>();
		sqls.add("insert into employee values('" + employeeID + "','" + password + "','" + username + "','" + apartmentID + "','" + employeeType.toString() + "')");
		helper.SQLDML(sqls);
		
		return true;
	}

	@Override
	public Employee login(String employeeID, String password) {
		SQLHelper helper = new SQLHelper();
		String sql = "select * from employee where employeeID='" + employeeID + "' and password='" + password + "'";
		List<HashMap<String, Object>> result = helper.getQueryList(sql);
		if (result == null || result.size() == 0) {
			return null;
		}
		HashMap<String, Object> map = result.get(0);
		Employee employee = new Employee(employeeID, password, (String)map.get("username"), (String)map.get("apartmentID"), EmployeeType.valueOf((String)map.get("employeeType")));
		return employee;
	}
}
